package com.example.kaiqiang.dagger2demo

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.android.synthetic.main.item_android.view.*

class MyAdapter constructor(list: List<FuckGoods>?, context: Context?) : RecyclerView.Adapter<MyAdapter.ViewHoder>() {
    private var mContext: Context? = context
    private var mList: List<FuckGoods>? = list
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHoder {
        return ViewHoder(LayoutInflater.from(mContext).inflate(R.layout.item_android, parent, false), mContext)
    }

    override fun getItemCount(): Int {
        return if ( mList==null||mList!!.isEmpty()) {
            0
        } else {
            mList!!.size
        }
    }

    fun upData(list: List<FuckGoods>?) {
        mList = list
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: ViewHoder, position: Int) {
        holder.bindView(mList!![position])
    }


    class ViewHoder(itemView: View, mContext: Context?) : RecyclerView.ViewHolder(itemView) {
        private var context: Context? = mContext
        fun bindView(entity: FuckGoods) {
            Glide.with(context).load(entity.url)//图片的url
                    .asBitmap()
                    .centerCrop()
                    //   .placeholder(DEFAULT_AVATAR) //默认的占位图片
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                    .into(itemView.iv_girl)
        }
    }
}